package com.myh.cc.coom.search.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.myh.cc.base.BaseApplication
import com.myh.cc.coom.search.bean.SearchBean
import com.myh.cc.databinding.ItemSearchBinding
import com.myh.cc.widget.OnItemClickListener

/**
 * des    ：
 * create    ：  mayahao.
 * date   ：  2018/1/15.   ：
 */
class SearchAdapter(bean: SearchBean): RecyclerView.Adapter<SearchAdapter.VH>() {

    val bean = bean
    var mListenre: OnItemClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchAdapter.VH {
        return VH.create(LayoutInflater.from(parent.context),parent)
    }

    override fun onBindViewHolder(holder: SearchAdapter.VH, position: Int) {

        val  list = bean.data.song.list

        val picUrl = "http://y.gtimg.cn/music/photo_new/T002R68x68M000"+list[position].albummid+".jpg?max_age=2592000"

        Glide.with(BaseApplication.context).load(picUrl).into(holder.binding.itemSearchIvImg)

        holder.binding.itemSearchTvMusicName.text = list[position].albumname
        holder.binding.itemSearchTvAuthName.text = list[position].singer[0].name

        holder.binding.root.setOnClickListener { view ->
            mListenre?.onClick(view,position)
        }

    }

    override fun getItemCount(): Int {
        return bean.data.song.list.size
    }

    class VH(binding: ItemSearchBinding): RecyclerView.ViewHolder(binding.root){

        val binding = binding

        companion object {

            fun create(inflater: LayoutInflater, parent: ViewGroup): SearchAdapter.VH {
                val binder = ItemSearchBinding.inflate(inflater, parent, false)
                return VH(binder)
            }
        }

    }
}